#include "../tools.h"


class Solution {
public:
    static bool cmp(const pair<string, int>& a, const pair<string, int>& b){
        if(a.second != b.second) return a.second > b.second;
        else return a.first < b.first;
    }
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> data;
        for(auto e: words){
            if(data.find(e) != data.end()){
                ++data[e];
            }
            else
                data[e] = 1;
        }

        vector<string> vec;
        for(auto i = data.begin(); i != data.end(); ++i){
            cout << i->first << " " << i->second << endl;
            vec.push_back(i->first);
        }
        sort(vec.begin(), vec.end(), [&](const string&a, const string& b)->bool{
            return data[a] == data[b] ? a < b : data[a] > data[b];
        });

        vec.erase(vec.begin() + k, vec.end());
        return vec;
    }
};

int main(int argc, char const *argv[])
{
    Solution sol;

    vector<string> strs = {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"};
    int k = 4;

    vector<string> res = sol.topKFrequent(strs, k);
    show(res);


    return 0;
}
